package com.example.administrator.qimo3no74;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.qimo3no74.goods.GoodsFragment;
import com.robertlevonyan.views.chip.Chip;
import com.robertlevonyan.views.chip.OnChipClickListener;

import java.lang.reflect.Method;

public class GoodsActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private Chip goodsChipAddShoppingCart;
    private Chip goodsChipPay;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);

        //初始化
        toolbar = findViewById(R.id.goodsToolbar);
        goodsChipAddShoppingCart = findViewById(R.id.goodsChipAddShoppingCart);
        goodsChipPay = findViewById(R.id.goodsChipPay);

        //加入购物车和立即购买点击事件
        goodsChipAddShoppingCart.setOnChipClickListener(new OnChipClickListener() {
            @Override
            public void onChipClick(View v) {
                Toast.makeText(v.getContext(), "成功加入购物车", Toast.LENGTH_SHORT).show();
            }
        });

        goodsChipPay.setOnChipClickListener(new OnChipClickListener() {
            @Override
            public void onChipClick(View v) {
                Toast.makeText(v.getContext(), "立即支付", Toast.LENGTH_SHORT).show();
            }
        });

        //标题栏
        addToobar();


        //主体内容
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.goodsFrameLayout, new GoodsFragment());
        fragmentTransaction.commit();


    }


    private void addToobar() {
        toolbar.setNavigationIcon(R.drawable.ic_chevron_left_black_24dp);
        toolbar.setTitle("");
        toolbar.inflateMenu(R.menu.menu_goods);//设置右上角的填充菜单
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_goods, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.menu_shopping_cart:
                Toast.makeText(this, "购物车", Toast.LENGTH_SHORT).show();
                /*Intent intent2 = new Intent(this, MainActivity.class);
                intent2.putExtra("id", 2);
                startActivity(intent2);*/
                break;
            case R.id.menu_home:
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                break;
            case R.id.menu_messages:
                Toast.makeText(this, "信息通知", Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_kefu:
                Toast.makeText(this, "我的客服", Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_feedback:
                Toast.makeText(this, "我要反馈", Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_share:
                Toast.makeText(this, "分享给你的好友哦", Toast.LENGTH_SHORT).show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    // 让菜单同时显示图标和文字
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return super.onMenuOpened(featureId, menu);
    }


    public void toStore(View view) {
        Toast.makeText(this,"您点了店铺",Toast.LENGTH_SHORT).show();
    }

    public void toKefu(View view) {
        Toast.makeText(this,"您点了客服",Toast.LENGTH_SHORT).show();
    }

    public void shoucang(View view) {
        Toast.makeText(this,"收藏成功",Toast.LENGTH_SHORT).show();
    }
}
